<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/aui-tab.js" ></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="find-head fixed" style="padding-top:0.4rem;">
        <div class="find-head-l" tapmode onclick="closeWin()"><i class="fa fa-angle-left"></i></div>
        <ul id="sort">
            <li onclick="changeSort('article')" id="article"><p>收藏资料</p></li>
            <li onclick="changeSort('goods')" id="goods"><p>收藏商品</p></li>
        </ul>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>

    <div id="list">

<!--         <div class="find-information-time">收藏时间：2018-12-30<span>11：22：33</span></div>
        <div class="find-information">
            <div class="find-information-t">
                <p>秋冬美白干货 | 黑白皮也能白成光的身体美白</p>
                208-11-08  10：20
            </div>
            <div class="find-information-m">
            <p>上个月去了趟海边，因为每天都是穿的露肩裙子和吊带，身上晒的好黑，一下子黑了两个色号</p>
                <div class="find-information-m-d">
                    <img src="image/find-information-m-d.png">
                </div>
            </div>
            <ul class="find-information-d">
                <li>
                    <i class="fa fa-star active"></i>
                    <p>收藏</p>
                </li>
                <li>
                    <i class="fa fa-download"></i>
                    <p>下载图片</p>
                </li>
                <li>
                    <i class="fa fa-clone"></i>
                    <p>复制文字</p>
                </li>
                <li>
                    <i class="fa fa-mail-forward"></i>
                    <p>分享</p>
                </li>
            </ul>
        </div> -->

<!--         <ul class="goods-list">
         <li>
             <img src="image/classify.jpg" alt="">
             <dl>
                 <dt>澳大利亚·毛孔脏东西浮出来·麦卢...</dt>
                 <dd>￥166</dd>
             </dl>
         </li> 
     </ul> -->

 </div>

</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/index.js"></script>
<script type="text/template" id="template">
    {{~it:value:index}}
    <div class="find-information-time">收藏时间：{{=value.created_at}}</div>
    <div class="find-information">
        <div class="find-information-t">
            <p>{{=value.title}}</p>
        </div>
        <div class="find-information-m">
            <p>{{=value.content}}</p>
            <div class="find-information-m-d">
                {{~value.files:valuee:indexx}}
                <img src="{{=valuee}}" onclick="seeThePic('{{=value.files}}',{{=indexx}});">
                {{~}}
            </div>
        </div>
        <ul class="find-information-d">
            <li onclick="collect({{=value.id}});">
                <i class="fa fa-star active" id="is_collection{{=value.id}}"></i>
                <p>收藏</p>
            </li>
            {{? value.type == 1}}
            {{? value.files.length}}
            <li onclick="downLoadPic('{{=value.files}}');">
                <i class="fa fa-download"></i>
                <p>下载图片</p>
            </li>
            {{??}}
            <li>
                <i class="fa fa-download"></i>
                <p>下载图片</p>
            </li>
            {{?}}
            {{??}}
            {{? value.files.length}}
            <li onclick="downLoadPic('{{=value.files}}');">
                <i class="fa fa-download"></i>
                <p>下载视频</p>
            </li>
            {{??}}
            <li>
                <i class="fa fa-download"></i>
                <p>下载视频</p>
            </li>
            {{?}}
            {{?}}
            <li onclick="copyContent('{{=value.content}}');">
                <i class="fa fa-clone"></i>
                <p>复制文字</p>
            </li>
            {{? value.type==1}}
            {{? value.files.length}}
            <li onclick="sharePic('{{=value.files}}');">
                <i class="fa fa-mail-forward"></i>
                <p>分享</p>
            </li>
            {{??}}
            <li>
                <i class="fa fa-mail-forward"></i>
                <p>分享</p>
            </li>
            {{?}}
            {{??}}
            {{? value.files.length}}
            <li onclick="shareVideo('{{=value.files}}');">
                <i class="fa fa-mail-forward"></i>
                <p>分享</p>
            </li>
            {{??}}
            <li>
                <i class="fa fa-mail-forward"></i>
                <p>分享</p>
            </li>
            {{?}}
            {{?}}
        </ul>
    </div>
    {{~}}
</script>
<!-- 收藏商品模板 -->
<script type="text/template" id="template_right">
    <ul class="goods-list">
        {{~it:value:index}}
        <li onclick="goods_details({{=value.id}})">
           <img src="{{=value.image}}" alt="">
           <dl>
               <dt>{{=value.title}}</dt>
               <dd>￥{{=value.price_current}}</dd>
           </dl>
       </li> 
       {{~}}
   </ul>
</script>

<script type="text/javascript">
    apiready = function(){

        sortName = 'article';
        $('#sort li').removeClass('active');
        $('#article').addClass('active');
        // 加载收藏资料
        loadData();

        pullLoading();
        pullRefresh();
        reloadListener();
    }

    // 监听事件 (页面刷新)
    function reloadListener(){
        api.addEventListener({
            name: 'new_reload',
        }, function(ret, err){
           loadGoodsData();
       });
    }

    function goods_details(id){
        api.openWin({
            name: 'goods_details',
            url: './goods_details.html',
            pageParam: {
                id: id
            }
        });
    }

    function loadData(more){
        // 进度提示框
        api.showProgress({
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

        if (more) {
            page += 1;
        }else{
            page = 1;
        }

        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/user/archive',
            method: 'get',
            data: {
                values: {
                    page: page,
                    row: row,
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // 关闭下拉刷新控件
                api.refreshHeaderLoadDone();
                if (more) {
                    templateFun(1,ret.data);
                }else{
                    templateFun(0,ret.data);
                }
            } else {
                // alert( JSON.stringify( err ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // 关闭下拉刷新控件
                // api.refreshHeaderLoadDone();
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun(more,data){
        var template = $api.byId('template');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var list = $api.byId('list');

        if (more) {

            if (data.length) {

                $api.append(list, html);
            }else{

             api.toast({
              msg: '没有更多了',
              duration: 2000,
              location: 'bottom'
          });
             return; 
         }
     }else{
        if (data.length) {
            $api.html(list, html);
        }else{
            var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无收藏资料</h2>';
            list.innerHTML = html;
        }
    }
}

    // 加载商品收藏
    function loadGoodsData(more){
        // 进度提示框
        api.showProgress({
            title: '努力加载中...',
            text: '请稍等...',
            modal: false
        });

        if (more) {
            page += 1;
        }else{
            page = 1;
        }

        var token = $api.getStorage('token');
        api.ajax({
            url: webAddress + '/api/user/product',
            method: 'get',
            data: {
                values: {
                    page: page,
                    row: row,
                },
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // 关闭下拉刷新控件
                api.refreshHeaderLoadDone();
                if (more) {
                    templateFun_right(1,ret.data);
                }else{
                    templateFun_right(0,ret.data);
                }
            } else {
                // alert( JSON.stringify( err ) );
                // 关闭上拉加载控件
                api.hideProgress();
                // 关闭下拉刷新控件
                // api.refreshHeaderLoadDone();
                api.toast({
                    msg: err.body.message,
                    duration: 2000,
                    location: 'bottom'
                });
                return false;
            }
        });
    }

    // 模板赋值函数
    function templateFun_right(more,data){
        // alert( JSON.stringify( data ) );

        var template = $api.byId('template_right');
        var dotFn = doT.template(template.innerHTML);
        var html = dotFn(data);
        var list = $api.byId('list');

        if (more) {

            if (data.length) {

                $api.append(list, html);
            }else{

             api.toast({
              msg: '没有更多了',
              duration: 2000,
              location: 'bottom'
          });
             return; 
         }
     }else{

        if (data.length) {

            $api.html(list, html);
        }else{

            var html = '<h2 style="padding:1rem; text-align:center; color:#999; font-size:0.5rem;">暂无收藏商品</h2>';
            list.innerHTML = html;
        }
    }
}


    // 预览图片
    function seeThePic(images,index){
        images = images.split(',');
        var photoBrowser = api.require('photoBrowser');

        photoBrowser.open({
            images: images,
            activeIndex: index,
            // placeholderImg: 'widget://res/img/apicloud.png',
            bgColor: '#000'
        }, function(ret, err) {
            if (ret) {
                // alert(JSON.stringify(ret));
                if (ret.eventType == 'click') {
                 photoBrowser.close(); 
             }
         } else {
                // alert(JSON.stringify(err));
                photoBrowser.close(); 
            }
        });
    }

    // 下载图片
    function downLoadPic(files){

        // 进度提示框
        api.showProgress({
            title: '下载中...',
            text: '请稍等...',
            modal: false
        });


        files = files.split(',');

        for (var i = files.length - 1; i >= 0; i--) {

            // 下载
            api.download({
                url : files[i],
                // savePath : 'fs://test.png',
                report : true,
                cache : true,
                allowResume : true
            }, function(ret, err) {
                if (ret.state == 1) {
                //下载成功
                api.saveMediaToAlbum({
                    path : ret.savePath
                }, function(ret, err) {
                    if (ret && ret.status) {
                        api.hideProgress();
                        api.toast({
                            msg: '下载成功',
                            duration: 2000,
                            location: 'bottom'
                        });                            
                    } else {
                        api.hideProgress();
                        api.toast({
                            msg: '下载失败',
                            duration: 2000,
                            location: 'bottom'
                        });                            
                    }
                });
            } else {
            }
        });
        }

    }

    // 下载视频
    function downLoadVideo(files){
        files = files.split(',');

        for (var i = files.length - 1; i >= 0; i--) {

            // 下载
            api.download({
                url : files[i],
                // savePath : 'fs://test.png',
                report : true,
                cache : true,
                allowResume : true
            }, function(ret, err) {
                if (ret.state == 1) {
                //下载成功
                api.saveMediaToAlbum({
                    path : ret.savePath
                }, function(ret, err) {
                    if (ret && ret.status) {

                        api.toast({
                            msg: '下载成功',
                            duration: 2000,
                            location: 'bottom'
                        });                            
                    } else {

                        api.toast({
                            msg: '下载失败',
                            duration: 2000,
                            location: 'bottom'
                        });                            
                    }
                });
            } else {
            }
        });
        }
    }

    // 复制文字
    function copyContent(content){

        var clipBoard = api.require('clipBoard');
        clipBoard.set({
            value: content
        }, function(ret, err) {
            if (ret) {
                api.toast({
                    msg: '复制成功',
                    duration: 2000,
                    location: 'bottom'
                });
                // clipBoard.get(function(ret, err) {
                //     if (ret) {
                //         // alert(JSON.stringify(ret));
                //     } else {
                //         // alert(JSON.stringify(err));
                //     }
                // });
            } else {
                // alert(JSON.stringify(err));
            }
        });

    }

    // 取消收藏
    function collect(id){
        var token = $api.getStorage('token');
        if (!token) {

            api.confirm({
                title: '提示!',
                msg: '是否登录?',
                buttons: ['确定', '取消']
            }, function(ret, err){
                if( ret ){
                    // alert( JSON.stringify( ret ) );
                    if (ret.buttonIndex == 1) {
                        api.openWin({
                            name: 'login',
                            url: './login.html',
                        });
                    }
                }else{
                    // alert( JSON.stringify( err ) );
                }
            });

        }else{
            api.ajax({
                url: webAddress + '/api/archive/'+id+'/collection',
                method: 'post',
                data: {
                    values: {
                    },
                },
                headers: {
                    Authorization: 'Bearer ' + token,
                    Accept: 'application/json',
                },
            },function(ret, err){
                if (ret) {
                    // alert( JSON.stringify( ret ) );
                    if (ret.status_code == 200) {
                        api.toast({
                            msg: ret.message,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }

                    loadData();

                } else {
                    alert( JSON.stringify( err ) );
                    if (err.statusCode == 400) {
                        api.toast({
                            msg: err.body.message,
                            duration: 2000,
                            location: 'bottom'
                        });
                        return false;
                    }
                }
            });
        }
    }

    // 分享图片
    function sharePic(files){

        api.showProgress({
            title: '正在获取图片...',
            text: '请稍候...',
            modal: false
        });

        files = files.split(',');
        imagePaths = [];
        for (var i = 0; i < files.length; i++) {

            // 下载
            api.download({
                url : files[i],
                report : false,
                cache : true,
                allowResume : false
            }, function(ret, err) {
                // alert(JSON.stringify(ret));

                if (ret.state == 1) {
                    //下载成功
                    imagePaths.push(ret.savePath);
                    if (imagePaths.length == files.length) {
                        // 关闭提示
                        api.hideProgress();
                        var sharedModule = api.require('shareAction');
                        sharedModule.share({
                            type: 'image',
                            images: imagePaths,
                            thumbnail: imagePaths,
                        });
                    }
                } else {

                }
            });
        }
    }

    // 分享视频
    function shareVideo(files){
        files = files.split(',');
        var sharedModule = api.require('shareAction');
        sharedModule.share({
            path: files[0],
            type: 'url'
        });
    }
    
    // 切换分类(资料馆/快抖云)
    function changeSort(sort){
        $('#sort li').removeClass('active');
        $('#'+sort).addClass('active');
        if (sort == 'article') {
            loadData();
            sortName = 'article';
        }else{
            loadGoodsData();
            sortName = 'goods';
        }
    }

    // 上拉加载
    function pullLoading(){
        api.addEventListener({
            name: 'scrolltobottom',
            extra: {
                threshold: 0
            }
        }, function(ret, err){
            if (sortName == 'article') {
                loadData(1);
                sortName = 'article';
            }else{
                loadGoodsData(1);
                sortName = 'goods';
            }
        });
    }

    // 下拉刷新
    function pullRefresh(){
        api.setRefreshHeaderInfo({
            // loadingImg: 'widget://image/refresh.png',
            bgColor: '#ccc',
            textColor: '#fff',
            textDown: '下拉刷新...',
            textUp: '松开刷新...'
        }, function(ret, err) {
            //在这里从服务器加载数据，加载完成后调用api.refreshHeaderLoadDone()方法恢复组件到默认状态
            if (sortName == 'article') {
                loadData();
                sortName = 'article';
            }else{
                loadGoodsData();
                sortName = 'goods';
            }
        });
    }

    function closeWin(){
        api.closeWin({
        });
    }
</script>
</html>